/**
 * 
 */
package edu.umd.clip.lm.factors;

import java.util.regex.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class CaseInfo implements WordToOvertFactorInterface {
	public static final String allCaps = "ALLCAPS";
	public static final String capitalized = "Cap";
	public static final String abbr = "Abbr.";
	public static final String nonWord = "non-word";
	public static final String regular = "reg";
	public static final String number = "num";
	public static final String datetime = "datetime";
	
	private static final Pattern cap_re = Pattern.compile("^[A-Z]");
	private static final Pattern nonWord_re = Pattern.compile("^[^a-zA-Z]+$");
	private static final Pattern num_re = Pattern.compile("^-?\\d[\\d.,/-]*(?:[eE]-?\\d+)?$");
	private static final Pattern datetime_re = Pattern.compile("^(?:\\d+(?:(?:[:/])\\d+){1,2}|'\\d{2,4}s?)$");
	
	public CaseInfo() {
	}
	
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.factors.WordToOvertFactorInterface#wordToOvertFactor(java.lang.String)
	 */
	@Override
	public String wordToOvertFactor(String word) {
		// prevent semicolons from being recognized as ALLCAPS
		word = word.replace("SEMICOLON", ":");
		if (num_re.matcher(word).matches()) return number;
		if (datetime_re.matcher(word).matches()) return datetime;
		if (nonWord_re.matcher(word).matches()) return nonWord;
		if (word.equals(word.toLowerCase())) return regular;
		if (cap_re.matcher(word).find()) {
			if (word.endsWith(".")) return abbr;
			if (word.equals(word.toUpperCase())) return allCaps;
			return capitalized;
		}
		// stuff like "mid-February" gets here
		return regular;
	}

}
